package com.open.platform.oauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.platform.oauth.entity.SysMenu;
import com.open.platform.oauth.entity.SysRoleMenu;
import com.open.platform.oauth.entity.SysUser;
import com.open.platform.oauth.entity.SysUserRole;
import com.open.platform.oauth.mapper.SysMenuMapper;
import com.open.platform.oauth.mapper.SysRoleMenuMapper;
import com.open.platform.oauth.mapper.SysUserMapper;
import com.open.platform.oauth.mapper.SysUserRoleMapper;
import com.open.platform.oauth.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Primary
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>implements SysUserService, UserDetailsService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> sysUserWrapper = Wrappers.<SysUser>query().lambda();
        sysUserWrapper.eq(SysUser::getUsername,username);
        SysUserMapper baseMapper = this.getBaseMapper();
        SysUser sysUser = baseMapper.selectOne(sysUserWrapper);
        if(Objects.isNull(sysUser)){
            return null;
        }
        //查询角色Id
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = Wrappers.<SysUserRole>query().lambda();
        sysUserRoleWrapper.eq(SysUserRole::getUserId,sysUser.getUserId());
        List<Integer> roles = sysUserRoleMapper.selectList(sysUserRoleWrapper).stream().map(sysUserRole -> sysUserRole.getRoleId()).collect(Collectors.toList());
        //查询权限1
        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuWrapper = Wrappers.<SysRoleMenu>query().lambda();
        sysRoleMenuWrapper.in(SysRoleMenu::getRoleId,roles);
        List<Integer> menus = sysRoleMenuMapper.selectList(sysRoleMenuWrapper).stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
        //查询权限2
        LambdaQueryWrapper<SysMenu> menuWrapper = Wrappers.<SysMenu>query().lambda();
        menuWrapper.in(SysMenu::getMenuId,menus);
        List<String> permissions = sysMenuMapper.selectList(menuWrapper).stream().map(sysMenu -> sysMenu.getPermission()).collect(Collectors.toList());
        //构建userDetails
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(permissions.toArray(new String[0]));
        UserDetails userDetails = User.withUsername(sysUser.getUsername())
                .password(sysUser.getPassword())
                .authorities(authorityList)
                .build();
        return userDetails;
    }
}
